/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Figure 3

Date Modified: 10/22/2019

*******************************************************************************/



*-------------------------------------------------------------------------------
                                   *LOAD DATA
*-------------------------------------------------------------------------------
use "$acs\acs_data", clear 

*-------------------------------------------------------------------------------
                                 *RESTRICT SAMPLE
*-------------------------------------------------------------------------------
keep if nonusbirth == 0 & father_sample != 1 & multi_sample != 1 & mult == 0 ///
	& female == 1 & oldc <=12 & inrange(age,18,40)==1
drop if citizen==1
keep if marst == 1 | marst == 2

*-------------------------------------------------------------------------------
                             *GENERATE DM (2008) VARIABLES
*-------------------------------------------------------------------------------

// RACE:
gen dmrace=0 if inlist(raced,100,120)==1 //White
replace dmrace = 1 if inlist(raced,200,210,801,917,985,986,990)==1 ///
					| inrange(raced,830,845)==1 ///
					| inrange(raced,901,904)==1 ///
					| inrange(raced,930,935)==1 ///
					| inrange(raced,950,955)==1 ///
					| inrange(raced,970,973)==1 ///
					| inrange(raced,980,983)==1
replace	dmrace = 2 if (inlist(raced,902,905,910,933,934,940,941,943,944,950,953,954,960,961)==1 ///
					| inrange(raced,400,679)==1 ///
					| inrange(raced,810,819)==1 ///
					| inrange(raced,831,838)==1 ///
					| inrange(raced,850,854)==1 ///
					| inrange(raced,860,887)==1 ///
					| inrange(raced,910,923)==1 ///
					| inrange(raced,926,930)==1 ///
					| inrange(raced,963,971)==1 ///
					| inrange(raced,973,981)==1 ///
					| inrange(raced,983,986)==1) & dmrace==.
replace dmrace = 3 if dmrace==.
label define dmrace_lbl 0 "White" 1"Black" 2"Asian" 3"Other"
label val dmrace dmrace_lbl		

// EDUCATION:
gen college = scol + cold // combined college variable

*-------------------------------------------------------------------------------
                      *SET UP EXCEL FILE THAT WILL STORE ESTIMATES
*-------------------------------------------------------------------------------
putexcel set "$foutput\figure3_data", replace
putexcel A1 = "year" 
putexcel B1 = "first" 
putexcel C1 = "all"
putexcel A2 = "1960"
putexcel A3 = "1970"
putexcel A4 = "1980"
putexcel A5 = "1990"
putexcel A6 = "2000"
putexcel A7 = "2008-2013"


*-------------------------------------------------------------------------------
                                 *REGRESSIONS
*-------------------------------------------------------------------------------
local controls "lths college" //leave out: hsdi
local controls "`controls' i.dmrace i.region age age2 age3" 
local controls "`controls' i.cohort " 
local wgt "perwtnorm"

// FIRST MARRIAGE
reg nchild chld1 `controls' if marrno==1 [aw=`wgt'], robust	
return list
local coef = r(table)[1, 1]
putexcel B7 = `coef'

// ALL MARRIAGES
reg nchild chld1 `controls' [aw=`wgt'], robust	
return list
local coef = r(table)[1, 1]
putexcel C7 = `coef'



*-------------------------------------------------------------------------------
                        *DM (2008) ESTIMATES TO 4 DECIMAL PLACES
*-------------------------------------------------------------------------------
use "$dm\sons-census", clear 	

// FIRST MARRIAGES
local i = 2
foreach groupvar of varlist year60 year70 year80 {
  di " "
  di "***** " "`groupvar'" " *****"
  quietly replace insamp=(marst==1 | marst==2) & marrno==1 & `groupvar'==1
  reg nchild ksex1 dd* black white asian educ1 educ2 age age2 age3 born* [pweight=perwt] if insamp
  return list
  local coef = r(table)[1, 1]
  putexcel B`i' = `coef'
  local i = `i' + 1
}

// ALL MARRIAGES	
local i = 2
foreach groupvar of varlist year60 year70 year80 year90 year00 {
  di " "
  di "***** " "`groupvar'" " *****"
  quietly replace insamp=(marst==1 | marst==2) & `groupvar'==1
  reg nchild ksex1 dd* black white asian educ1 educ2 age age2 age3 born* [pweight=perwt] if insamp
  return list
  local coef = r(table)[1, 1]
  putexcel C`i' = `coef'
  local i = `i' + 1
}



*-------------------------------------------------------------------------------
					               *FIGURE CODE
*-------------------------------------------------------------------------------
import excel "$foutput\figure3_data", clear firstrow
replace all = round(all, 0.0001)
replace first = round(first, 0.0001)
replace all = -.0030000001 if year == "2008-2013"
format all first %9.4f
graph bar all first, over(year) ///
	legend(position(11) ring(0) col(1) lab(1 "All marriages") lab(2 "First marriages")) ///
	ytitle("Fertility Effect of First Child Girl") ///
	graphregion(color(white)) bgcolor(white) ///
	bar(1, color(gs5) lwidth(thick)) bar(2, color(gs10) lwidth(thick)) blabel(total, size(vsmall) format(%5.4f))
graph export "$foutput\figure3.png", replace	

